package com.yiji.fintech.musfinance.common.eventbus.listener;

import com.acooly.core.utils.enums.ResultStatus;
import com.google.common.eventbus.Subscribe;
import com.yiji.fintech.musfinance.base.enums.NotifyStatusEnum;
import com.yiji.fintech.musfinance.base.enums.TransStatusEnum;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import com.yiji.fintech.musfinance.common.annotation.ListenerAnnotation;
import com.yiji.fintech.musfinance.common.context.SpringApplicationContext;
import com.yiji.fintech.musfinance.common.domain.WithdrawDomain;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.enums.ReviewStatusEnum;
import com.yiji.fintech.musfinance.common.eventbus.event.WithdrawEvent;
import com.yiji.fintech.musfinance.common.eventbus.listener.base.AbstractCommonListener;
import com.yiji.fintech.musfinance.common.strategy.context.SendGeteWayContext;
import com.yiji.fintech.musfinance.common.strategy.gateway.SendGateWayWithdrawStrategy;
import com.yiji.fintech.musfinance.entity.InstBank;
import com.yiji.fintech.musfinance.service.InstBankService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 提现监听器
 * Created by ouwen@yiji.com} on 2017/9/26.
 */
@ListenerAnnotation(value = "withdrawListener")
@Scope("prototype")
@Service
public class WithdrawListener extends AbstractCommonListener<WithdrawEvent,WithdrawDomain> {
    @Value("${center.check.ischeck}")
    private String ischeck;

    @Subscribe
    @Override
    public void doExcute(WithdrawEvent event) {
        super.doExcute(event);
    }

    @Override
    protected void sendgetway(BaseDomain domain, ResultFacade result) {
        SendGateWayWithdrawStrategy withdrawStrategy = (SendGateWayWithdrawStrategy) SpringApplicationContext.getBeanByClass(SendGateWayWithdrawStrategy.class);
        SendGeteWayContext context = new SendGeteWayContext(withdrawStrategy);
        context.doSendGateWay(domain,result);
    }

    @Override
    protected void initTransaction(BaseDomain domain) {
        WithdrawDomain withdrawDomain = (WithdrawDomain)domain;
        if("0".equals(ischeck)){//人工审核
            withdrawDomain.setReviewStatus(ReviewStatusEnum.UNREVIEW);//设置审核状态
            withdrawDomain.setTransStatus(TransStatusEnum.INITIAL);
        }else{
            withdrawDomain.setReviewStatus(ReviewStatusEnum.REVIEW_SUCCESS);
        }
        //初始化收款方银行信息
        InstBankService instBankService = (InstBankService)SpringApplicationContext.getBeanByClass(InstBankService.class);
        InstBank instBank = instBankService.unique(withdrawDomain.getRecBankId());
        if(instBank != null){
            withdrawDomain.setRecBankName(instBank.getInstBankName());
            withdrawDomain.setRecUnionBankNo(instBank.getUnionBankNo());
        }
        withdrawDomain.setStatus(ResultStatus.processing);
        withdrawDomain.setNotifyStatus(NotifyStatusEnum.NO);
        withdrawDomain.setGatewayTime(new Date());
    }
}
